<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>杨澎锴的实践作业26-vue.js事件mit</title>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>

  <body>
    <h1>vuejs事件</h1>
    <section>
      <div id="app">
        <h2>加法</h2>
        <!-- 自定义一个命名事件，调用方法 -->
        <add-method :a="6" :b="12" @add_event="getResult"></add-method>
        <p>{{result}}</p>
      </div>
      <script>
        Vue.component("add-method", {
          props: ["a", "b"],
          template: '<div><button @click="add">加法</button></div>',
          methods: {
            add: function () {
              let value = 0;
              value = this.a + this.b;
              //   使用$方法来进行返回
              this.$emit("add_event", {
                result: value,
              });
            },
          },
        });

        var app7 = new Vue({
          el: "#app",
          data: {
            result: 0,
          },
          methods: {
            getResult: function (val) {
              this.result = val.result;
            },
          },
        });
      </script>
    </section>
  </body>
</html>
